﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject;
using System.Collections.Generic;
using log4net;
using Jalal.Library.Utility;

public partial class Admin_CMS : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindControls();
        }
    }

    private void BindControls()
    {
        List<CMS.CMSPages> cmsPages = CMS.GetCMSPageList();
        ddlCMSTitle.Items.Add(new ListItem("Select", "0"));
        foreach(CMS.CMSPages cmsPage in cmsPages)
        {
            ddlCMSTitle.Items.Add(new ListItem(cmsPage.ToString().Replace("_", " "),((int)cmsPage).ToString()));
        }
    }

    protected void CMS_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddlCMSTitle.SelectedValue != "0")
            {
                CMS cms = CMS.GetCMSById(int.Parse(ddlCMSTitle.SelectedValue));
                lblCMSTitle.Text = cms.CMSTitle;
                fckCMSContent.Value = cms.CMSContent;
                lblLastUpdate.Text = Jalal.Library.Utility.CommonUtility.FormatedDateString(cms.LastUpdate);
            }
        }
        catch (Exception ex)
        {
            // Write the log into handled exception list
            ILog log = LogHelper.GetHandledFileLogger("CMS");
            if (log.IsErrorEnabled)
                log.Error("Failed to load CMS", ex);
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                CMS cms = CMS.GetCMSById(int.Parse(ddlCMSTitle.SelectedValue));
                cms.CMSContent = fckCMSContent.Value;
                cms.LastUpdate = DateTime.Now;
                if (cms.Update())
                    uccResponse.SetSuccessMessage("Information Saved");
                else
                    uccResponse.SetFailMessage("Failed to save information, please try again");
            }
            catch (Exception ex)
            {
                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("CMS");
                if (log.IsErrorEnabled)
                    log.Error("Failed to save CMS", ex);
            }
        }
    }
}
